<template>

  <div>

     <h2>用户注册</h2>
  </div>

   <div>
  <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
  >

    <el-form-item label="用户账号" prop="name">
      <el-input v-model="ruleForm.name" />
    </el-form-item>
    <el-form-item label="用户账号" prop="username">
      <el-input v-model="ruleForm.username" />
    </el-form-item>

    <el-form-item label="用户账号" prop="password">
      <el-input v-model="ruleForm.password" />
    </el-form-item>

    <el-form-item label="用户性别" prop="sex">
      <el-radio-group v-model="ruleForm.sex">
        <el-radio value="0">男</el-radio>
        <el-radio value="1">女</el-radio>
      </el-radio-group>
    </el-form-item>

    <el-upload
        class="avatar-uploader"
        action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
    </el-upload>


    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleForm)">登录</el-button>
      <el-button @click="resetForm(ruleForm)">重置</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>

<script setup lang="js">
 import { ref, reactive, toRefs } from 'vue'
  import { ElMessage } from 'element-plus'
  import { ElForm, ElFormItem, ElInput, ElButton, ElRadioGroup, ElRadio, ElUpload, ElIcon } from 'element-plus'
  import { Plus } from '@element-plus/icons-vue'

  const formSize = ref('default')
  const ruleFormRef = ref(null)
  const ruleForm = reactive({
    name: '',
    username: '',
    password: '',
    sex: '',
    avatar: '',
  })
  const rules = reactive({
    name: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
    ],
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
    ],
    sex: [
      { required: true, message: '请选择性别', trigger: 'change' },
    ],
  })
  const submitForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid) => {
      if (valid) {
        ElMessage.success('登录成功')
      } else {
        console.log('error submit!')
        return false
      }
    })
  }
  const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
  }
</script>

<style scoped>

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>